package com.marvels.financial.pay.hlbpay.util;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 银行code
 * @author wujunfeng
 *
 */
public enum BankCodeEnum {

	ICBC("中国工商银行","ICBC"),
	ABC("中国农业银行","ABC"),
	CMBCHINA("招商银行","CMBCHINA"),
	CCB("中国建设银行","CCB"),
	BOCO("交通银行","BOCO"),
	BOC("中国银行","BOC"),
	CMBC("中国民生银行","CMBC"),
	CGB("广发银行","CGB"),
	HXB("华夏银行","HXB"),
	POST("中国邮政储蓄银行","POST"),
	ECITIC("中信银行","ECITIC"),
	CEB("中国光大银行","CEB"),
	PINGAN("平安银行","PINGAN"),
	CIB("兴业银行","CIB"),
	SPDB("浦发银行","SPDB"),
	BCCB("北京银行","BCCB"),
	BON("南京银行","BON"),
	NBCB("宁波银行","NBCB"),
	BEA("东亚银行","BEA"),
	SRCB("上海农商银行","SRCB"),
	SHB("上海银行","SHB"),
	CZB("浙商银行","CZB"),
	TCCB("天津银行","TCCB"),
	HSBANK("徽商银行","HSBANK"),
	HFBANK("恒丰银行","HFBANK"),
	CBHB("渤海银行","CBHB"),
	JSB("江苏银行","JSB"),
	CITI("花旗银行","CITI"),
	THX("贵阳银行","THX"),
	HANGSENGBANK("恒生银行","HANGSENGBANK"),
	GDNYBANK("南粤银行","GDNYBANK"),
	LZBANK("兰州银行","LZBANK");
	
	
	private String bankName;
	
	private String code;
	
	BankCodeEnum(String bankName, String code) {
		this.bankName = bankName;
		this.code = code;
	}

	public String getBankName() {
		return bankName;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	/**
	 * 根据银行卡名称获取code
	 * 因为没有一套合适的，所以先开发一套简单的
	 * @param bankName
	 * @return
	 */
	public static String getCode(String bankName){
		// 1.找出最相似的code
		double maxSimilarity = 0.0; // 最大相似度
		String maxSimilarityCode = null; // 最大相似度的银行卡名称
		for (BankCodeEnum bankCodeEnum : BankCodeEnum.values()) {
			// 1.1 如果能完全匹配上，直接退出
			if(StrUtil.equals(bankCodeEnum.getBankName(), bankName)){
				maxSimilarityCode = bankCodeEnum.getCode();
				break;
			}
			
			// 1.2 计算相识度
			int temp = bankCodeEnum.getBankName().replace("银行", "").length();
			int temp2 = longestCommonSubstring(bankCodeEnum.getBankName().replace("银行", ""), bankName.replace("银行", "")).length();
			
			double similarity = NumberUtil.div(temp2, temp) * temp2;// * temp2 更精确
			if(similarity > maxSimilarity && (NumberUtil.div(temp2, temp) > 0.5D || NumberUtil.div(temp2, bankName.replace("银行", "").length()) > 0.5D)){
				maxSimilarity = similarity;
				maxSimilarityCode = bankCodeEnum.getCode();
			}
		}
		System.out.println("传入银行卡名称：" + bankName + "，匹配出的code：" + maxSimilarityCode);
		// 如果没匹配到，给默认值
		return StrUtil.nullToDefault(maxSimilarityCode, "BOC");
	}
	
	public static String longestCommonSubstring(String strA, String strB) {
		char[] chars_strA = strA.toCharArray();
		char[] chars_strB = strB.toCharArray();
		int m = chars_strA.length;
		int n = chars_strB.length;

		// 初始化矩阵数据,matrix[0][0]的值为0， 如果字符数组chars_strA和chars_strB的对应位相同，则matrix[i][j]的值为左上角的值加1， 否则，matrix[i][j]的值等于左上方最近两个位置的较大值， 矩阵中其余各点的值为0.
		int[][] matrix = new int[m + 1][n + 1];
		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= n; j++) {
				if (chars_strA[i - 1] == chars_strB[j - 1]) {
					matrix[i][j] = matrix[i - 1][j - 1] + 1;
				} else {
					matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);
				}
			}
		}

		// 矩阵中，如果matrix[m][n]的值不等于matrix[m-1][n]的值也不等于matrix[m][n-1]的值， 则matrix[m][n]对应的字符为相似字符元，并将其存入result数组中。
		char[] result = new char[matrix[m][n]];
		int currentIndex = result.length - 1;
		while (matrix[m][n] != 0) {
			if (matrix[m][n] == matrix[m][n - 1]) {
				n--;
			} else if (matrix[m][n] == matrix[m - 1][n]) {
				m--;
			} else {
				result[currentIndex] = chars_strA[m - 1];
				currentIndex--;
				n--;
				m--;
			}
		}
		return new String(result);
	}
	
	
}
